package com.pj.wisdomlogistics.ui.activity;

import android.net.Uri;
import android.os.Environment;
import android.widget.RelativeLayout;

import com.artifex.mupdfdemo.FilePicker;
import com.artifex.mupdfdemo.MuPDFCore;
import com.artifex.mupdfdemo.MuPDFPageAdapter;
import com.artifex.mupdfdemo.MuPDFReaderView;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.Log;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class PdfActivity extends BaseActivity {
    @BindView(R.id.rel_content)
    RelativeLayout mLayout;

    private MuPDFCore core;
    private MuPDFReaderView mDocView;
    private String mFilePath;

    @Override
    public void initView() {

        Uri uri = Uri.fromFile(new File(getDownloadDir() , "stamp.pdf"));
        core = openFile(uri.getPath());
        if (core != null && core.countPages() == 0) {
            core = null;
        }
        if (core == null || core.countPages() == 0 || core.countPages() == -1) {
            Log.e("Tag", "Document Not Opening");
        }
        if (core != null) {
            mDocView = new MuPDFReaderView(this) {
                @Override
                protected void onMoveToChild(int i) {
                    if (core == null)
                        return;
                    super.onMoveToChild(i);
                }

            };
            mDocView.setAdapter(new MuPDFPageAdapter(this, new FilePicker.FilePickerSupport() {
                @Override
                public void performPickFor(FilePicker picker) {

                }
            }, core));
            mLayout.addView(mDocView);
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_pdf;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        ActivityManagerUtil.create().finishActivity();
    }

    public static final String getDownloadDir() {
        File pbcDownload = new File(Environment.getExternalStorageDirectory(), "StampPdf");
        if (!pbcDownload.exists()) {
            pbcDownload.mkdir();
        }
        return pbcDownload.getAbsolutePath();
    }

    @Override
    public void onDestroy() {
        if (core != null)
            core.onDestroy();
        core = null;
        super.onDestroy();
    }

    private MuPDFCore openFile(String path) {
        int lastSlashPos = path.lastIndexOf('/');
        mFilePath = new String(lastSlashPos == -1
                ? path
                : path.substring(lastSlashPos + 1));
        try {
            core = new MuPDFCore(this, path);
            // New file: drop the old outline data
        } catch (Exception e) {
            Log.e("Tag", e.getMessage());
            return null;
        }
        return core;
    }
}
